import Vue from "vue"

import VueRouter from 'vue-router'

Vue.use(VueRouter)

import Vuex from 'vuex'
Vue.use(Vuex);

var car = JSON.parse(localStorage.getItem('car') || '[]');
var store = new Vuex.Store({
    state: { //this.$store.state.***
        //{id:商品的id,count:要购买的数量,price:商品的单价,selected:true保存商品是否被选中的状态}
        car: car //将购物车中的商品的数据用一个数组存储起来
    },
    mutations: { //this.$store.commit('方法名称','按需传递唯一的参数')
        //把商品信息加入到car中
        //这个参数state是固定的
        addToCar(state, goodsinfo) {
            var flag = false;
            state.car.some(item => {
                if (item.id == goodsinfo.id) {
                    item.count += parseInt(goodsinfo.count);
                    flag = true;
                    return true;
                }
            });

            if (!flag) {
                state.car.push(goodsinfo);
            }
            //当更新car之后，把car数组，存储到本地的localstorage中
            localStorage.setItem('car', JSON.stringify(state.car));
        },
        updateGoodsInfo(state, goodsinfo) {
            state.car.some(item => {
                if (item.id === goodsinfo.id) {
                    item.count = parseInt(goodsinfo.count);
                    return true;
                }
            });
            //当更新car之后，把car数组，存储到本地的localstorage中
            localStorage.setItem('car', JSON.stringify(state.car));
        },
        removeFormCar(state, id) {
            state.car.some((item, i) => {
                if (item.id == id) {
                    state.car.splice(i, 1);
                    return true;
                }
            });
            //当更新car之后，把car数组，存储到本地的localstorage中
            localStorage.setItem('car', JSON.stringify(state.car));
        },
        updateGoodsSelected(state, info) {
            state.car.some(item => {
                if (item.id === info.id) {
                    item.selected = info.selected;
                }
            });
            //当更新car之后，把car数组，存储到本地的localstorage中
            localStorage.setItem('car', JSON.stringify(state.car));
        }
    },
    getters: { //this.$store.getters.***
        getAllCount(state) {
            var c = 0;
            state.car.forEach(item => {
                c += item.count;
            });
            return c;
        },
        getGoodsCount(state) {
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.count;
            });
            return o;
        },
        getGoodsSelected(state) {
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.selected;
            });
            return o;
        },
        getGoodsCountAndAmount(state) {
            var o = {
                count: 0,
                amount: 0
            }
            state.car.forEach(item => {
                if(item.selected){
                    o.count += item.count;
                    o.amount += item.count * item.price
                }
            });
            return o;
        }
    }
});

import moment from 'moment'
//定义全局的过滤器
Vue.filter('dateFormat', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
    return moment(dataStr).format(pattern);
})

//导入vue-preview
import './lib/CSS/golbal.css'
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

//导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.http.options.root = 'http://192.168.230.1:3030/' //這個是配置全局的默認路由路徑
Vue.http.options.emulateJSON = true

//按需导入mint-ui的组件
// import {
//     Header
// } from 'mint-ui'
// Vue.component(Header.name, Header)
// import { Lazyload } from 'mint-ui';
// Vue.use(Lazyload);
// import {
//     Swipe,
//     SwipeItem,
//     Button
// } from 'mint-ui';
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name,Button);

import MintUI from 'mint-ui';
Vue.use(MintUI);
import 'mint-ui/lib/style.css'

import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

import app from "./App.vue"
import router from './router.js'

var vm = new Vue({
    el: '#app',
    render: c => c(app),
    router,
    store //状态管理对象
})